const app = getApp();
var interval = null //倒计时函数
Page({
  data:{
    time: '获取验证码',
    currentTime: 61,
    pwd:'',
    code:'',
    phone:''
  },
  //得到phone
  getphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 得到code
  getcode:function(e){
    this.setData({
      code: e.detail.value
    })
  },
  // 得到支付密码
  getpwd: function (e) {
    if (e.detail.value.length == 6) {
      this.setData({
        pwd: e.detail.value
      })
    }
  },
  // 发送验证码
  sendCode: function () {
    const _this = this;
    this.settimeD();
    this.setData({
      disabled: true
    })
    wx.request({
      url: app.globalData.path + 'services/app/applets/SendSMSCode',
      data: {
        phoneNumber: _this.data.phone,
        SendType: 5
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data)
        console.log(res.data.result.securityCode)
        if (res.data.success) {
          _this.setData({
            securityCode: res.data.result.securityCode
          })
          if (res.data.result.sendResultType == 2) {
            wx.showToast({
              title: '手机号不存在',
              icon: 'none',
              duration: 2000
            })
          } else if (res.data.result.sendResultType == 3) {
            wx.showToast({
              title: '操作频繁',
              icon: 'none',
              duration: 2000
            })
          } else if (res.data.result.sendResultType == 4 || res.data.result.sendResultType == 5) {
            wx.showToast({
              title: '短信模板错误',
              icon: 'none',
              duration: 2000
            })
          }
        } else {
          wx.showToast({
            title: '发送失败，请重试',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //倒计时
  settimeD: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
  toNext: function (e) {
    const _this = this;
    if (e.detail.value.phone == ''){
      wx.showModal({
        content: '请填写手机号！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }else if (_this.data.code == '') {
      wx.showModal({
        content: '请输入验证码！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }else if (_this.data.pwd == '') {
      wx.showModal({
        content: '请填写支付密码！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }
    if (this.data.securityCode) {
      wx.request({
        url: app.globalData.path + 'services/app/applets/ChangePhoneNumber',
        data: {
          phoneNumber: e.detail.value.phone,
          payPassword: _this.data.pwd,
          code: _this.data.code,
          securityCode: _this.data.securityCode
        },
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': 'Bearer ' + app.globalData.authorization
        },
        success: function (res) {
          console.log(res.data)
          if(res.data.success){
              wx.switchTab({
                url: '../../../my/my'
              })
          } else {
            wx.showToast({
              title: res.data.error.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
})